package io.a.opencv;

import org.bytedeco.javacv.FFmpegFrameGrabber;

import javax.imageio.ImageIO;
import java.io.File;

public class FrameExtract {
    public static void main(String[] args) {
        FFmpegFrameGrabber grabber = null;
        try {
//            String file = "rtsp://admin:Zjlab@2022@10.0.108.100:554/Streaming/Channels/101";
            String file = "rtsp://10.10.40.209:8554/1";
            grabber = FFmpegFrameGrabber.createDefault(file);
            grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式，不然会丢包很严重
            grabber.setImageWidth(1920);
            grabber.setImageHeight(1080);
            System.out.println("grabber start");
            grabber.startUnsafe();

//2.帧截图
            int i = 0;
            while (i < 1) {
                File outPut = new File("d:/" + i + ".jpeg");
                org.bytedeco.javacv.Frame frame = grabber.grabImage();
                if (frame != null) {
                    ImageIO.write(OpencvUtil.FrameToBufferedImage(frame), "jpeg", outPut);
                    System.out.println("图片已保存");
                    Thread.sleep(1000);
                    i++;
                }

            }
            grabber.stop();
            grabber.release();
        } catch (Exception e) {
            System.out.println(e);
        } finally {

        }
    }
}
